package com.xiaoq.feature.endpoint;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.feature.entity.CommentEntity;
import com.xiaoq.feature.service.ICommentService;
import com.xiaoq.feature.vo.CommentReptileVo;
import com.xiaoq.feature.vo.CommentVo;
import com.xiaoq.vo.PagingVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(tags={"必吃评论相关接口"})
@RestController
@RequestMapping("/api/v1/sites")
public class CommentEndpoint {
	
	@Autowired
	private ICommentService commentSer;
	
	@Autowired
	private IdGenerator idGenerator;
	
	private static final Logger LOG = LoggerFactory.getLogger(CommentEndpoint.class);
	
	private class CommentListResponse extends GenericListResponse<CommentVo>{}
	
	
	@ApiOperation(value = "获取景区附近的特色必吃评论", notes = "获取景区附近的特色必吃评论")
	@ApiResponses({ @ApiResponse(code = 200, response = CommentListResponse.class, message = "获取景区附近的特色必吃评论") })
	@GetMapping(value = "/feature/comment", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> findCommentList(
			@RequestParam(value="shopId",required=true)@ApiParam(value="店铺Id",required=true)String shopId,
			@RequestParam(value="tag",required=false)@ApiParam(value="店铺标签",required=false)String tag,
			@ModelAttribute(value="pvo")PagingVo pvo){
		
 		List<CommentEntity> commentList = commentSer.findCommentList(shopId, tag, pvo);
 		List<CommentVo> commentVoList = new ArrayList<>();
 		CommentVo commentVo = null;
 		for(CommentEntity comment : commentList){
 			commentVo = new CommentVo();
 			BeanUtils.copyProperties(comment, commentVo);
 			commentVoList.add(commentVo);
 		}
 		Long total = (Long) pvo.getTc();
 		return CommentListResponse.listAndCount(commentVoList, total);
 	
	}
	
	@ApiOperation(value = "（爬虫专用）评论对接爬虫", notes = "爬虫专用）评论对接爬虫")
	@ApiResponses({ @ApiResponse(code = 200, response = CommentListResponse.class, message = "爬虫专用）评论对接爬虫") })
	@PostMapping(value = "/feature/comment/reptile", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> commentRepTile(@RequestBody CommentReptileVo commentReptileVo){
		CommentEntity comment = new CommentEntity();
		BeanUtils.copyProperties(commentReptileVo, comment);
		comment.setId(idGenerator.nextId());
		comment.setPubTime(System.currentTimeMillis());
		commentSer.addComment(comment);
		
		return GenericResponse.ok();
		
 		
	}

}
